package org.ix.dynamicapis.configure.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

@Slf4j
public class TypeParseUtils {

    public static Object getRealValue(String type, Object object) {
        if (StringUtils.isBlank(type)) return object;
        if (object == null) return null;
        switch (type.toLowerCase()) {
            case "bool":
            case "boolean":
                return parseBoolean(object);
            case "char":
            case "character":
                return parseChar(object);
            case "byte":
                return parseByte(object);
            case "short":
                return parseShort(object);
            case "integer":
            case "int":
                return parseInt(object);
            case "long":
                return parseLong(object);
            case "float":
                return parseFloat(object);
            case "double":
                return parseDouble(object);
            case "string":
                return parseString(object);
            case "bigdecimal":
            case "decimal":
                return parseBigDecimal(object);
            default:
                return object;
        }
    }

    public static Boolean parseBoolean(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Boolean) return (Boolean) obj;
        try {
            return Boolean.parseBoolean(obj + "");
        } catch (Exception e) {
            log.error("转换类型Boolean失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Character parseChar(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Character) return (Character) obj;
        try {
            String s = obj + "";
            if (s.length() == 1) {
                return s.charAt(0);
            }
        } catch (Exception e) {
            log.error("转换类型Character失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Byte parseByte(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Byte) return (Byte) obj;
        try {
            return Byte.parseByte(obj + "");
        } catch (Exception e) {
            log.error("转换类型Byte失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Short parseShort(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Short) return (Short) obj;
        try {
            return Short.parseShort(obj + "");
        } catch (Exception e) {
            log.error("转换类型Short失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Integer parseInt(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Integer) return (Integer) obj;
        try {
            return Integer.parseInt(obj + "");
        } catch (Exception e) {
            log.error("转换类型Integer失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Long parseLong(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Long) return (Long) obj;
        try {
            return Long.parseLong(obj + "");
        } catch (Exception e) {
            log.error("转换类型Long失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Float parseFloat(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Float) return (Float) obj;
        try {
            return Float.parseFloat(obj + "");
        } catch (Exception e) {
            log.error("转换类型Float失败, 转换的值：" + obj);
        }
        return null;
    }

    public static Double parseDouble(Object obj) {
        if (obj == null) return null;
        if (obj instanceof Double) return (Double) obj;
        try {
            return Double.parseDouble(obj + "");
        } catch (Exception e) {
            log.error("转换类型Double失败, 转换的值：" + obj);
        }
        return null;
    }

    public static String parseString(Object obj) {
        if (obj == null) return null;
        if (obj instanceof String) return (String) obj;
        try {
            return obj + "";
        } catch (Exception e) {
            log.error("转换类型String失败, 转换的值：" + obj);
        }
        return null;
    }

    public static BigDecimal parseBigDecimal(Object obj) {
        if (obj == null) return null;
        if (obj instanceof BigDecimal) return (BigDecimal) obj;
        try {
            return new BigDecimal(obj.toString());
        } catch (Exception e) {
            log.error("转换类型BigDecimal失败, 转换的值：" + obj);
        }
        return null;
    }
}
